//
//  FindVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-12.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "FindVC.h"
#import "MyRequest.h"
#import "Find.h"
#import "YouHuiVC.h"
#import "FindWebVC.h"
#import "UIImageView+AFNetworking.h"

#define BASE_URL @"http://www.shihuo.cn/app_swoole_general/find?&token=e10adc3949ba59abbe56e057f20f883e"
#define BASE_DIAN @"http://m.shihuo.cn/shop"
#define BASE_GUANG @"http://m.shihuo.cn/find"
#define BASE_CAI @"http://m.shihuo.cn/special/index?id=11"

@interface FindVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * arr1;
@property (nonatomic, strong) NSMutableArray *arr2;
@end

@implementation FindVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"发现";
    _arr1 = [[NSMutableArray alloc] init];
    _arr2 = [[NSMutableArray alloc] init];
    
    [self setTableView];
    
    [self getData];

}
- (void)setTableView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr1.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Find *find = nil;
    if (indexPath.section == 0) {
        find = _arr1[indexPath.row];
    }else
    {
        find = _arr2[indexPath.row];
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:find.img_url]];
    UIImage *image = [UIImage imageWithData:data];
    CGSize size = CGSizeMake(30, 30);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
    [image drawInRect:rect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    cell.textLabel.text = find.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0 ) {
        YouHuiVC *vc = [[YouHuiVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        FindWebVC *vc = [[FindWebVC alloc] init];
        if (indexPath.section == 0 && indexPath.row == 1) {
            vc.url = BASE_DIAN;
        }else if (indexPath.section == 1&& indexPath.row == 0) {
            vc.url = BASE_GUANG;
        } else {
            vc.url = BASE_CAI;
        }
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 1;
}
- (void)getData
{
    [MyRequest getWithURL:BASE_URL parameters:nil success:^(NSDictionary *dic) {
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *dic in arr[0]) {
            Find *find = [[Find alloc] initWithDictionary:dic];
            [_arr1 addObject:find];
        }
        for (NSDictionary *dic in arr[1]) {
            Find *find = [[Find alloc] initWithDictionary:dic];
            [_arr2 addObject:find];
        }
        [_tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
